Skip to content

Conversation

@LeoLiang000
Copy link
Collaborator

新增浏览器状态存储功能:保存前置用例执行后的浏览器状态,避免重复执行相同前置操作

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @LeoLiang000, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此PR引入了浏览器状态持久化功能,允许在测试用例执行后保存浏览器状态(如cookies、localStorage、sessionStorage),并在后续用例中加载这些状态。这显著减少了重复的前置操作,提高了测试效率,尤其适用于需要登录或其他复杂设置的场景。通过引入快照机制,测试可以从一个预设的浏览器状态开始,从而加速测试执行并简化测试脚本。

Highlights

  • 浏览器状态持久化: 引入了浏览器状态(如cookies、localStorage、sessionStorage)的保存和加载功能,以支持测试用例的夹具(fixture)模式。
  • 配置更新: 在 config_run.yaml.example 中增加了 snapshotuse_snapshot 字段的配置示例,用于定义和使用浏览器状态快照。
  • 新增核心模块: 新增 webqa_agent/browser/context_manager.py 文件,提供了管理浏览器上下文持久化状态的工具类,支持原子写入和文件锁。
  • 用例结构扩展: 修改了 Case 模型,增加了 snapshotuse_snapshot 字段,允许用例定义为夹具或加载预设状态。
  • 用例执行流程优化: 重构了用例执行器,将用例分为夹具用例(串行执行并保存状态)和普通用例(并发执行并可加载状态),从而避免重复的前置操作。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 新增了浏览器状态存储功能,类似于测试中的 fixtures,可以保存和复用浏览器状态,避免重复执行前置用例(如登录),从而提升测试效率。这是一个非常有用的功能。

整体实现质量不错,特别是 PersistentContextManager 中对并发安全(filelock)和原子写入(临时文件+重命名)的考虑很周全。

代码审查发现了一些可以改进的地方:

  1. case_executor.py 中存在大量重复代码,主要是在处理“固定用例”和“普通用例”的执行逻辑上。这部分建议重构以提高代码的可读性和可维护性。
  2. context_manager.py 中的一个异步方法 get_storage_state_path 实际上是同步的,建议修改。
  3. 代码中存在局部导入和未使用的函数参数,建议清理。

所有提出的修改建议均与“并行测试执行器应支持在同一测试会话中运行具有不同浏览器配置的测试”的规则不冲突,且有助于提升代码质量和可维护性。具体的修改建议已在代码评论中给出。

Comment on lines +68 to +71
async def get_storage_state_path(
snapshot_id: str,
base_dir: str = 'webqa_agent/browser/browser_context'
) -> Optional[str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

get_storage_state_path 方法被声明为 async,但其内部并未执行任何 await 操作。此函数中的所有操作(如 .exists(), open(), json.load())都是同步的。建议将其修改为常规的同步方法,以避免混淆。同时,这也需要移除 case_executor.py 中对此方法的 await 调用。

Suggested change
async def get_storage_state_path(
snapshot_id: str,
base_dir: str = 'webqa_agent/browser/browser_context'
) -> Optional[str]:
def get_storage_state_path(
snapshot_id: str,
base_dir: str = 'webqa_agent/browser/browser_context'
) -> Optional[str]:

snapshot_id = case.get('snapshot')
if snapshot_id:
try:
from webqa_agent.browser.context_manager import PersistentContextManager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

PersistentContextManager 在两个不同的方法中被局部导入(此处和第337行)。根据 PEP 8 的规范,最好将此导入移至文件顶部。这样做可以避免重复导入,并使模块的依赖关系更加清晰,从而提高代码的可读性。

Comment on lines +319 to +324
async def _load_fixture_state(
self,
session: BrowserSession,
case: Dict[str, Any],
case_config: Dict[str, Any]
) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

_load_fixture_state 方法签名中定义了 case_config 参数,但在方法体内并未使用。为了简化代码并避免混淆,应移除此未使用的参数。调用处(第291行)也需要相应修改。

    async def _load_fixture_state(
        self,
        session: BrowserSession,
        case: Dict[str, Any]
    ) -> None:

@LeoLiang000 LeoLiang000 merged commit 9c6c5a5 into dev_0.2.3 Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants